package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.customerAction.service.CustomerOrdeCreateSer;
import com.cloudlead.customerService.vo.CreateCustmoerOrderVo;
import com.cloudlead.customerService.vo.FleetPriceVo;
import com.cloudlead.customerService.vo.ForwardPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123 on 2015/8/31.
 */
@Service
public class CustomerOrderCreateSerImpl implements CustomerOrdeCreateSer {

    @Autowired
    TbCustomerOrderService customerOrderService;
    @Autowired
    BaseCompanyService   baseCompanyService;
    @Autowired
    RegionService regionService;
    @Autowired
    BaseVoyageService baseVoyageService;
    @Autowired
    BaseShipRouteService baseShipRouteService;
    @Autowired
    BaseDockService baseDockService;
    @Autowired
    TbPlacePriceService placePriceService;
    @Autowired
    BasePortService basePortService;
    @Autowired
    BaseShipRoutePriceService baseShipRoutePriceService;
    @Autowired
    TbContainerInfoService tbContainerInfoService;

    @Autowired
    TbFleetOrderService tbFleetOrderService;


    @Transactional
    public String createOrder(CreateCustmoerOrderVo vo) {
            TbCustomerOrder tbCustomerOrder=new TbCustomerOrder();//货主订单
            TbFleetOrder startFleetOrder=new TbFleetOrder();//起始港拖车运单
            TbFleetOrder destFleetOrder=new TbFleetOrder();//目的港拖车运单
            TbPlacePrice startPrice=new TbPlacePrice();//起始港报价
            TbPlacePrice destPrice=new TbPlacePrice();//目的港报价
            BaseShipRoutePrice baseShipRoutePrice=new BaseShipRoutePrice();;//航线报价
            Double total=new Double(0);//订单总价
            BaseDock startDock=new BaseDock();//起始码头
            BaseDock destDock=new BaseDock();//目的码头
            Region startRegion=new Region();//起始区域
            Region destRegion=new Region();//目的区域
            BaseVoyage baseVoyage=new BaseVoyage();//航次
            if (vo.getVoyageId()!=null&&!"".equals(vo.getVoyageId())){

                baseVoyage.setId(vo.getVoyageId());//航次ID
                tbCustomerOrder.setBaseVoya(baseVoyage);
            }


            tbCustomerOrder.setCommodity(vo.getCommodity());//货物名称
            tbCustomerOrder.setQuantity(vo.getConVolume());//箱量
            tbCustomerOrder.setDelName(vo.getfDo());//收货人地址
            tbCustomerOrder.setPolName(vo.getsDo());//发货人地址
            //创建公司
            BaseCompany baseCompany=new BaseCompany();
            baseCompany.setId(vo.getBaseCompany());
            tbCustomerOrder.setBaseCompany(baseCompany);

            tbCustomerOrder.setConsignee(vo.getConsigneePhone());//收货人联系方式
            if(vo.getShipPrice()!=null&&!"".equals(vo.getShipPrice())) {
                 baseShipRoutePrice=baseShipRoutePriceService.view(vo.getShipPrice());
                tbCustomerOrder.setForwarderPrice(baseShipRoutePrice);//货代报价
                total+=vo.getShipPriceTotal();
            }

            if (vo.getStartFleetPriceTotal()!=null) {
                startPrice=placePriceService.view(vo.getStartFleetPrice());
                total += vo.getStartFleetPriceTotal();//计算订单总价
                tbCustomerOrder.setPolCarPrice(startPrice);
            }
            if (vo.getDestFleetPriceTotal() !=null) {
                 destPrice=placePriceService.view(vo.getDestFleetPrice());
                 total += vo.getDestFleetPriceTotal();//计算订单总价
                 tbCustomerOrder.setDelCarPrice(destPrice);
              }

              tbCustomerOrder.setTotalPrice(total);//订单总价
              customerOrderService.create(tbCustomerOrder);//插入货主订单

            //起始港车队订单
            if(vo.getStartFleetPrice()!=null&&!"".equals(vo.getStartFleetPrice())) {
                destDock.setId(vo.getStartFleetDock());//码头
                startRegion.setId(vo.getStartFleetRegion());//区域
                startFleetOrder.setQuantity(vo.getConVolume());//箱量
                startFleetOrder.setIsStart("1");//出口
                startFleetOrder.setFleetCompany(startPrice.getBaseCompany());//车队
                startFleetOrder.setFlag("0");//删除标记
                if (baseVoyage.getId()!=null&&!"".equals(baseVoyage.getId()))
                      startFleetOrder.setBaseVoya(baseVoyage);//航次
                startFleetOrder.setDock(startDock);//码头
                startFleetOrder.setPrice(vo.getStartFleetPriceTotal());//目的港车队总价

                startFleetOrder.setTbCustomerOrder(tbCustomerOrder);//货主订单
                tbFleetOrderService.create(startFleetOrder);

            }

            //目的港车队订单
            if(vo.getDestFleetPrice()!=null&&!"".equals(vo.getDestFleetPrice())) {
                destDock.setId(vo.getDestFleetDoock());//码头
                destRegion.setId(vo.getDestFleetRegion());//区域
                destFleetOrder.setQuantity(vo.getConVolume());//箱量
                destFleetOrder.setIsStart("0");//进口
                destFleetOrder.setFleetCompany(destPrice.getBaseCompany());//车队
                destFleetOrder.setFlag("0");//删除标记
                if (baseVoyage.getId()!=null&&!"".equals(baseVoyage.getId()))
                     destFleetOrder.setBaseVoya(baseVoyage);//航次
                destFleetOrder.setDock(destDock);//码头
                destFleetOrder.setPrice(vo.getDestFleetPriceTotal());//目的港车队总价
                destFleetOrder.setTbCustomerOrder(tbCustomerOrder);//货主订单
                tbFleetOrderService.create(destFleetOrder);
            }



        //插入箱信息

        for (int i=0;i<vo.getVolume().length;i++){//遍历想类型和箱数量
            for (int j=0;j<vo.getVolume()[i];j++){

                Double startFleetPrice = new Double(0);//起始港车队箱单价
                Double destFleetPrice=new Double(0);//目的港车队箱单价
                Double shipPrice=new Double(0);//货代箱单价

                if ("20GP".equals(vo.getConType()[i])){

                    startFleetPrice=startPrice!=null?startPrice.getGp20():null;//起始车队20GP单价
                    destFleetPrice=destPrice!=null?destPrice.getGp20():null;//目的车队20GP单价
                    shipPrice=baseShipRoutePrice.getGp20();
                }else if ("40GP".equals(vo.getConType()[i])){

                    startFleetPrice=startPrice!=null?startPrice.getGp40():null;//起始车队20GP单价
                    destFleetPrice=destPrice!=null?destPrice.getGp40():null;//目的车队20GP单价
                    shipPrice=baseShipRoutePrice.getGp40();
                }else if ("40HC".equals(vo.getConType()[i])){

                    startFleetPrice=startPrice!=null?startPrice.getHc40():null;//起始车队20GP单价
                    destFleetPrice=destPrice!=null?destPrice.getHc40():null;//目的车队20GP单价
                    shipPrice=baseShipRoutePrice.getHc40();
                }else if ("40TK".equals(vo.getConType()[i])){

                    startFleetPrice=startPrice!=null?startPrice.getTk40():null;//起始车队20GP单价
                    destFleetPrice=destPrice!=null?destPrice.getTk40():null;//目的车队20GP单价
                    shipPrice=baseShipRoutePrice.getTk40();
                }
                //起始港箱子
                if(startFleetOrder.getId()!=null){
                    TbContainerInfo startContainer=new TbContainerInfo();
                    startContainer.setTbCustomerOrder(tbCustomerOrder);//货主订单
                    startContainer.setFleetOrder(startFleetOrder);//车队订单
                    startContainer.setType(vo.getConType()[i]);//箱型
                    startContainer.setCarPrice(startFleetPrice);
                    startContainer.setForwarderPrice(shipPrice);
                    tbContainerInfoService.create(startContainer);
                }
                //目的箱子
                if(destFleetOrder.getId()!=null){
                    TbContainerInfo destContainer=new TbContainerInfo();
                    destContainer.setTbCustomerOrder(tbCustomerOrder);//货主订单
                    destContainer.setFleetOrder(destFleetOrder);//车队订单
                    destContainer.setType(vo.getConType()[i]);//箱型
                    destContainer.setCarPrice(destFleetPrice);
                    destContainer.setForwarderPrice(shipPrice);
                    tbContainerInfoService.create(destContainer);
                }
                //货主箱子
                if(startFleetOrder.getId()==null&&destFleetOrder.getId()==null){
                    TbContainerInfo container=new TbContainerInfo();
                    container.setTbCustomerOrder(tbCustomerOrder);//货主订单
                    container.setType(vo.getConType()[i]);//箱型
                    container.setForwarderPrice(shipPrice);
                    tbContainerInfoService.create(container);
                }


            }

        }

            return "创建成功";
        }

    @Override
    public Page<BaseShipRoute> findAllRoute() {

        PageRequest pr = new PageRequest();
        Filter filter = new Filter(new Criteria(new Term("flag","0")));
        pr.setFilter(filter);
        Page<BaseShipRoute> pb = (Page<BaseShipRoute>)baseShipRouteService.findAll(pr);
        return pb;
    }

    @Override
    public Page<BaseShipRoutePrice> findAllRoutePrice(ForwardPriceVo vo, int rows, int page) {
        PageRequest pr = new PageRequest();
        pr.setPage(page);
        pr.setSize(rows);
        List<Term> tr=new ArrayList<Term>();
        if(vo.getRouteId()!=null&&!"".equals(vo.getRouteId()))//航线ID
            tr.add(new Term("baseShipRoute.id",vo.getRouteId()));

        tr.add(new Term("flag","0"));
        Filter filter = new Filter(new Criteria(tr));

        pr.setFilter(filter);
        Page<BaseShipRoutePrice> pb = (Page<BaseShipRoutePrice>)baseShipRoutePriceService.findAll(pr);
        return pb;
    }

    @Override
    public Page<BaseDock> findAllDockByPortName(String portName) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();

        if (portName!=null&&!"".equals(portName)) {
            tr.add(new Term("portName", portName));
            Filter filter = new Filter(new Criteria(tr));
            pr.setFilter(filter);
        }
        Page<BaseDock> pb = (Page<BaseDock>)baseDockService.findAll(pr);
        return pb;
    }

    @Override
    public Page<Region> findAllPlace(String cityName) {

        BasePort port=basePortService.findOne(new Filter(new Criteria(new Term("portName",cityName))));
        PageRequest pr = new PageRequest();
        if (port!=null&&port.getCity()!=null){
            Filter filter = new Filter(new Criteria(new Term("parentCode",port.getCity().getCode())));
            pr.setFilter(filter);
        }

        Page<Region> pb = (Page<Region>)regionService.findAll(pr);
        return pb;
    }

    @Override
    public Page<TbPlacePrice> findAllPlacePrice(FleetPriceVo vo, int rows, int page) {
        PageRequest pr = new PageRequest();

        pr.setPage(page);
        pr.setSize(rows);

        List<Term> tr=new ArrayList<Term>();
        if(vo.getPlace()!=null&&!"".equals(vo.getPlace()))//区域ID
            tr.add(new Term("basePlace.id",vo.getPlace()));
        if(vo.getDock() !=null&&!"".equals(vo.getDock()))//码头ID
            tr.add(new Term("baseDock.id",vo.getDock()));

        tr.add(new Term("flag","0"));
        Filter filter = new Filter(new Criteria(tr));
        pr.setFilter(filter);
        Page<TbPlacePrice> pb = (Page<TbPlacePrice>)placePriceService.findAll(pr);
        return pb;
    }
}
